package com.jtjrenren.android.taxi.passenger.core.wallect.balance;

import android.app.Activity;
import android.content.Intent;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.bean.PayInfo;
import com.jtjrenren.android.taxi.passenger.bean.User;
import com.jtjrenren.android.taxi.passenger.core.pay.charge.ChargeActivity;
import com.jtjrenren.android.taxi.passenger.core.wallect.recharge.RechargeHistoryActivity;
import com.jtjrenren.android.taxi.passenger.core.web.AppShowHtmlActivity;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.utils.StringUtils;
import com.library.weidget.ClearEditText;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by wdl on 2016/1/27.
 */
public class BalaceActivity extends BaseActivity {

    @BindView(R.id.wallect_balance_money)
    TextView tv_money;
    @BindView(R.id.title)
    TextView tv_title;
    @BindView(R.id.wallect_balance_otherprice)
    ClearEditText editText;
    @BindView(R.id.wallect_balance_price30)
    RadioButton rb_01;
    @BindView(R.id.wallect_balance_price50)
    RadioButton rb_02;
    @BindView(R.id.wallect_balance_price100)
    RadioButton rb_03;

    private User currentUser;
    private String money, checkedMoney; //充值金额

    @Override
    public int getLayoutId() {
        return R.layout.aty_wallect_balance;
    }

    @Override
    public void init() {
        tv_title.setText("钱包余额");
        currentUser = AppManger.getInstance().user;

        tv_money.setText("¥" + currentUser.getRMBMoney());
    }

    @OnClick(R.id.title_left)
    public void back() {
        finish();
    }

    @OnCheckedChanged(R.id.wallect_balance_price30)
    void onChecked30(boolean checked) {
        if (checked)
            checkedMoney = money = "30";
    }

    @OnCheckedChanged(R.id.wallect_balance_price50)
    void onChecked40(boolean checked) {
        if (checked)
            checkedMoney = money = "50";
    }

    @OnCheckedChanged(R.id.wallect_balance_price100)
    void onChecked50(boolean checked) {
        if (checked)
            checkedMoney = money = "100";
    }

    @OnTextChanged(R.id.wallect_balance_otherprice)
    void onTextChanged(CharSequence text) {
        String tmp = text.toString().trim();
        if (StringUtils.isEmpty(tmp)) {
            money = checkedMoney;
        } else
            money = tmp;
    }

    @OnClick(R.id.wallect_balance_otherprice)
    void clcikOther() {
        rb_01.setChecked(false);
        rb_02.setChecked(false);
        rb_03.setChecked(false);
    }

    private static final int DECIMAL_DIGITS = 2;//小数的位数

    @OnTextChanged(value = R.id.wallect_balance_otherprice, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChangedd(CharSequence s) {
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > DECIMAL_DIGITS) {
                s = s.toString().subSequence(0,
                        s.toString().indexOf(".") + DECIMAL_DIGITS + 1);
                editText.setText(s);
                editText.setSelection(s.length());
            }
        }
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            editText.setText(s);
            editText.setSelection(2);
        }
        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                editText.setText(s.subSequence(0, 1));
                editText.setSelection(1);
                return;
            }
        }
    }

    @OnClick(R.id.wallect_balance_usedesc)
    public void clickDesc() {
        Intent intent = new Intent(this, AppShowHtmlActivity.class);
        intent.putExtra(AppShowHtmlActivity.EXTRAS_TITLE, getString(R.string.yueshiyongshuoming));
        intent.putExtra(AppShowHtmlActivity.EXTRAS_URL, ModuleUtils.getHtmlUrl(getString(R.string.html_bs_balance)));
        startActivity(intent);
    }

    @OnClick(R.id.wallect_balance_chognzhimingxi)
    public void clickCzMx() {
        startActivity(new Intent(this, RechargeHistoryActivity.class));
    }

    @OnClick(R.id.wallect_balance_quchognzhi)
    public void clickQCz() {
        if (StringUtils.isEmpty(money) || (!StringUtils.isEmpty(money) && Double.parseDouble(money) <= 0)) {
            displayShort("充值金额不能为空");
            return;
        }
        PayInfo payInfo = new PayInfo(PayInfo.TYPE_CHARGE);
        payInfo.setPay_price(money);
//        startActivityForResult(PayActivity.getDiyIntent(this,payInfo), Constants.INTENT_REQUEST_CODE_PAY);
//        startActivity(PayActivity.getDiyIntent(this, payInfo));
        startActivity(ChargeActivity.getDiyIntent(this, money));
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constants.INTENT_REQUEST_CODE_PAY:
                if (resultCode == Activity.RESULT_OK) {

                }
                break;
        }
    }
}
